package com.fml.dataStructure;

/**
 * 自定义线性表，数据结构
 */
public class MyList {

    private final int MAX_SIZE = 20;
    private int [] elements;
    private int length;

    public int[] getElements() {
        return elements;
    }

    public void setElements(int[] elements) {
        this.elements = elements;
        this.length = elements.length;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int get(int index) {

        if (length <= index || index < 0 || index > MAX_SIZE) {
            throw new ArrayIndexOutOfBoundsException("索引范围不符 index= " + index);
        }
        return elements[index];
    }








    public static void main(String[] args) {

        MyList myList = new MyList();
        myList.setElements(new int[]{1, 2, 3, 4, 5});
        printListByIndex(myList,2);
        printListByIndex(myList,5);
    }

    private static void printListByIndex(MyList list, int i) {
        System.out.println(list.get(i));

    }
}
